﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using P.InOut.NetMvc.Properties;
using P.InOut.NetMvc.Models;
using P.InOut.NetMvc.Common;
using BitFactory.Logging;

namespace P.InOut.NetMvc.Logging
{
    public class InOutDatabaseLogger : AbstractDatabaseLogger
    {
        protected override bool DoLog(LogEntry logEntry)
        {
            // Cast Param
            InOutLogEntry cLogEntry = (InOutLogEntry)logEntry;

            // Create db context and dispose when completed
            using (InOutDataContext db =
                new InOutDataContext(Settings.Default.dbConn))
            {
                // Create new log instance
                P.InOut.NetMvc.Models.Log log = new Log();

                // Assign Properties
                log.UserName        = cLogEntry.UserName;
                log.Application     = cLogEntry.Application;
                log.Category        = cLogEntry.Category.ToString();
                log.Message         = cLogEntry.Message;
                log.DateTimeEntered = cLogEntry.Date;
                log.Severity        = cLogEntry.Severity.ToString();
                log.SeverityDescr   = cLogEntry.SeverityString;

                // Insert Log Entry
                db.Logs.InsertOnSubmit(log);
                db.SubmitChanges();
            }

            return true;
        }

        public override bool Execute(string application, string category, 
            string message, DateTime datetime, LogSeverity severity)
        {
            InOutLogEntry entry = new InOutLogEntry();
            entry.Application   = application;
            entry.Category      = category;
            entry.Message       = message;
            entry.Date          = datetime;
            entry.Severity      = severity;

            bool result = false;
            result = DoLog(entry);

            return result;
        }

        public bool Execute(string application, string category,
            string message, DateTime datetime, LogSeverity severity, string userName)
        {
            InOutLogEntry entry = new InOutLogEntry();
            entry.Application   = application;
            entry.Category      = category;
            entry.Message       = message;
            entry.Date          = datetime;
            entry.Severity      = severity;
            entry.UserName      = userName;

            bool result = false;
            result = DoLog(entry);

            return result;
        }
    }
}
